﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Autenticacao
{
    public partial class frme_cons : Form
    {
        System.Data.SqlClient.SqlConnection con;

        public Exploracao ex2;
        public string nomec;
        
        public frme_cons(Exploracao ex1)
        {
            this.ex2 = ex1;

            InitializeComponent();
        }

        private void cmdae_ipc_Click(object sender, EventArgs e)
        {
            if (txtae_especie.Text == "" || txtae_nchip.Text == "")
            {
                MessageBox.Show("É necessário inserir os dados básicos do animal", "Faltam dados", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                con = new System.Data.SqlClient.SqlConnection();
                con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                con.Open();

                SqlCommand command1 = con.CreateCommand();
                command1.CommandText = "SELECT MAX(id_aniexp) FROM Animais_exploracao";
                SqlCommand command2 = con.CreateCommand();
                command2.CommandText = "SELECT Count(*) FROM Animais_exploracao";
                int num1 = (Int32)command2.ExecuteScalar();
                int num = 0;
                if (num1 == 0)
                    num = 1;
                else
                {
                    num = (Int32)command1.ExecuteScalar();
                    num++;
                }
                con.Close();

                con.Open();
                int ide = ex2.get_id();
                SqlCommand command3 = con.CreateCommand();
                command3.CommandText = "SET IDENTITY_INSERT Animais_exploracao ON Insert into Animais_exploracao (id_aniexp, especie, chip, id_exploracao) values (" + num + ", '" + txtae_especie.Text + "', '" + txtae_nchip.Text + "', " + ide + ") SET IDENTITY_INSERT Animais_exploracao OFF";
                command3.CommandType = CommandType.Text;
                SqlDataReader reader1 = command3.ExecuteReader();
                con.Close();

                MessageBox.Show("Animal Registado com Sucesso!", "Próximo painel", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                int idc = ex2.get_idcliente();
                con.Open();
                SqlCommand command4 = con.CreateCommand();
                command4.CommandText = "SELECT nome FROM Cliente WHERE id_cliente = "+idc+"";
                SqlDataReader readatrib = command4.ExecuteReader();
                while (readatrib.Read())
                {
                    nomec = readatrib["nome"].ToString();
                }
                con.Close();
                string chip = txtae_nchip.Text;

                ConsultasInserir_Exp frm1 = new ConsultasInserir_Exp(ex2, num, nomec, chip);
                this.Hide();
                frm1.ShowDialog();
            }
        }

        private void cmdae_fechar_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
